function Figure2

Nfactors = 3;

addpath('basic_functions')
addpath('../Data')

[Return,Z,IsF,Nt,Chars,CharsNames] = package_data_all_greeks;
Return = Return.ret_daily;

[N,T,L] = size(Z);

%% Base case model
% construct W and X matrices that will be used for IPCA
W = NaN(L,L,T);
X = NaN(L,T);
for t = 1:T-1
    W(:,:,t) = squeeze(Z(IsF(:,t+1),t,:))'*squeeze(Z(IsF(:,t+1),t,:)) / Nt(t+1);
    X(:,t+1) = squeeze(Z(IsF(:,t+1),t,:))'*Return(IsF(:,t+1),t+1) / Nt(t+1);
    Return(~IsF(:,t+1),t+1) = NaN;
end
clear t

[G, F] = IPCA(X,W,Nt,Nfactors);

sqG2 = sqrt(sum(G.^2,2));
[X,ia] = sortrows(sqG2(1:end-1),-1);

figure(1)
set(gcf,'Position',[1 1 750 1500])
subplot(3,1,1)
GG = G(1:end-1,1);
NN = CharsNames;
[X,ia] = sortrows(GG,-1);
bar((1:size(GG,1)),GG(ia,1))
set(gca,'xtick',(1:size(GG,1)),'xticklabel',NN(ia),'XTickLabelRotation',270,'ylim',[-0.35 0.5])
title('Factor # 1')

subplot(3,1,2)
GG = G(1:end-1,2);
[X,ia] = sortrows(GG,-1);
bar((1:size(GG,1)),GG(ia,1))
set(gca,'xtick',(1:size(GG,1)),'xticklabel',NN(ia),'XTickLabelRotation',270,'ylim',[-0.35 0.5])
title('Factor # 2')

subplot(3,1,3)
GG = G(1:end-1,3);
[X,ia] = sortrows(GG,-1);
bar((1:size(GG,1)),GG(ia,1))
set(gca,'xtick',(1:size(GG,1)),'xticklabel',NN(ia),'XTickLabelRotation',270,'ylim',[-0.35 0.5])
title('Factor # 3')

saveas(gcf,'figures_outputs/Figure2.eps','epsc')
saveas(gcf,'figures_outputs/Figure2.jpg','jpg')